package XynLeen.GraduationProject.Activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.classic.adapter.BaseAdapterHelper;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import XynLeen.GraduationProject.Bean.PhotoBean;
import XynLeen.GraduationProject.R;
import XynLeen.GraduationProject.view.BaseRecycleListActivity;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

public class Activity_Album_Photo extends BaseRecycleListActivity<PhotoBean> implements View.OnClickListener {

    private String typeid;

    private TextView mTvNavigation;
    private ImageButton mIbNavigation_back;
    private ImageButton mIbNavigation_add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        typeid=getIntent().getStringExtra("typeid");

        mTvNavigation=findViewById(R.id.tv_navigation_label);
        mIbNavigation_back=findViewById(R.id.ib_navigation_back);
        mIbNavigation_add=findViewById(R.id.ib_navigation_add);

        mTvNavigation.setText("上传照片");
        mIbNavigation_back.setOnClickListener(this);
        mIbNavigation_add.setOnClickListener(this);
    }

    @Override
    public void doConvert(BaseAdapterHelper helper,final PhotoBean item, int position) {
        ImageView imageView=helper.getView(R.id.image);
        Glide.with(this).load(item.getUrl()).into(imageView);
        LinearLayout linearLayout=helper.getView(R.id.item);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Activity_Album_Photo.this, Activity_Album_Photo_Show.class);
                intent.putExtra("title","照片");
                intent.putExtra("url", item.getUrl());
                startActivity(intent);
            }
        });
        linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                final AlertDialog dialog = new AlertDialog.Builder(Activity_Album_Photo.this)
                        .setTitle("提示")//设置对话框的标题
                        .setMessage("确定删除吗？")//设置对话框的内容
                        //设置对话框的按钮
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                DataSupport.delete(PhotoBean.class, item.getId());
                                getdata();
                                dialog.dismiss();
                            }
                        }).create();

                dialog.show();
                return false;
            }
        });
    }

    @Override
    public int getAdapterItemId() {
        return R.layout.activity_album_photo_item;
    }

    @Override
    public int getLayoutId() {
        return R.layout.adapter_recycleview_album;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void onResume() {
        super.onResume();
        getdata();
    }

    public void getdata() {
        List<PhotoBean> list = DataSupport.where("typeid=?",typeid).find(PhotoBean.class);
        mAdapter.clear();
        mAdapter.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void initView() {

    }

    public RecyclerView.LayoutManager getlayoutManager() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        return gridLayoutManager;
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_add, menu);
//        return super.onCreateOptionsMenu(menu);
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//
//        switch (item.getItemId()) {
//            case R.id.add:
//                takePicture();
//                break;
//        }
//        return super.onOptionsItemSelected(item);
//    }


    public void takePicture() {
        int selectedMode = MultiImageSelectorActivity.MODE_MULTI;

        boolean showCamera = true;
        int maxNum = 6;

        Intent intent = new Intent(this, MultiImageSelectorActivity.class);
        // 是否显示拍摄图片
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, showCamera);
        // 最大可选择图片数量
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, maxNum);
        // 选择模式
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, selectedMode);
        // 默认选择
        startActivityForResult(intent, 9);
    }

    List<String> selectList = new ArrayList<>();
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case 9:
                    selectList = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                    if (null != selectList && selectList.size() > 0) {
                        for (int i = 0; i <selectList.size() ; i++) {
                            PhotoBean bean = new PhotoBean();
                            bean.setTypeid(typeid);
                            bean.setUrl(selectList.get(i));
                            bean.save();
                        }
                        getdata();
                    }
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_navigation_back:
//                startActivity(new Intent(this, Activity_Album.class));  加上这个会重载一遍页面？？？
                finish();
                break;
            case R.id.ib_navigation_add:
                takePicture();
                break;

        }
    }

}
